package com.example.animactivity;

import android.content.Intent;
import android.os.Bundle;
import android.transition.Slide;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity2 extends AppCompatActivity {

    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ImageView img2 = findViewById(R.id.img2);
        ViewCompat.setTransitionName(img2, "shared element");

//        ActivityCompat.postponeEnterTransition(this);
//        ActivityCompat.startPostponedEnterTransition(this);
        // 创建一个GestureDetector实例
        mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                // 在这里处理下滑手势逻辑
//                if (e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 200) {
//                    // 处理下滑手势
//                    Toast.makeText(MainActivity2.this, "下滑手势", Toast.LENGTH_SHORT).show();
//                    return true;
//                }
                return false;
            }

            @Override
            public boolean onScroll(@Nullable MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
                if (e2.getY() - e1.getY() > 100) {
//                    // 用户向下滑动
                    Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
                    ViewCompat.setTransitionName(img2, "shared element");
                    // TransOneActivity, 启动方法没变;
                    startActivity(
                            intent,
                            ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity2.this, img2, "shared element").toBundle());

                    return true;

                }
                return false;

            }
        });
        // 例如
//        ActivityCompat.postponeEnterTransition(this);
//        ActivityCompat.startPostponedEnterTransition(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }
}